package com.huilian.iotbox.admin.wap.controller;

import com.huilian.iotbox.admin.wap.service.MerchantService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.MerchantDto;
import com.huilian.iotbox.data.enums.ErrorMsgEnum;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.po.Merchant;
import com.huilian.iotbox.data.vo.UserInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * @author 谢洛涛
 * @date 2021/3/16 17:26
 */
@RestController
@RequestMapping("/api/merchant")
public class MerchantController extends UserInfoController {
    @Autowired
    private MerchantService merchantService;

    /**
     * 获取商户上传的资料（未上传）
     *
     * @param request
     * @return
     */
    @GetMapping("/uncommittedMerchantInfo")
    public ServerResponse getUncommittedMerchantInfo(HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return merchantService.getUncommittedMerchantInfo(userInfo);
    }

    /**
     * 更新商户上传的资料（未上传）
     *
     * @param merchantDto
     * @param request
     * @return
     */
    @PutMapping("/uncommittedMerchantInfo")
    public ServerResponse updateUncommittedMerchantInfo(@RequestBody MerchantDto merchantDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantService.updateUncommittedMerchantInfo(userInfo, merchantDto);
        return ServerResponse.createBySuccess();
    }

    /**
     * 获取分账进件商户上传的资料（未上传）
     */
    @GetMapping("/uncommittedSubAccountMerchantInfo")
    public ServerResponse getUncommittedSubAccountMerchantInfo(HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return merchantService.getUncommittedSubAccountMerchantInfo(userInfo);
    }

    /**
     * 更新商户上传的资料（未上传）
     */
    @PutMapping("/uncommittedSubAccountMerchantInfo")
    public ServerResponse updateUncommittedSubAccountMerchantInfo(@RequestBody MerchantDto merchantDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return merchantService.updateUncommittedSubAccountMerchantInfo(userInfo, merchantDto);
    }

    /**
     * 商户进件-H5端
     *
     * @param merchantDto
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/merchantRegister")
    public ServerResponse merchantRegister(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return merchantService.merchantRegister(merchantDto, userInfo);
    }

    /**
     * 商户进件-微信小程序端
     *
     * @param merchantDto
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/merchantRegisterByApplet")
    public ServerResponse merchantRegisterByApplet(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return merchantService.merchantRegisterByApplet(merchantDto, userInfo);
    }

    /**
     * 分账商户进件-微信小程序端
     *
     * @param merchantDto
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/subLedgerMerchantRegisterByApplet")
    public ServerResponse subLedgerMerchantRegisterByApplet(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return merchantService.subLedgerMerchantRegisterByApplet(merchantDto, userInfo);
    }

    /**
     * 各类进件图片上传接口-H5
     *
     * @param image
     * @param type
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadphoto")
    public ServerResponse merchantRegister(@RequestBody MultipartFile image, @Param(value = "type") String type) throws Exception {
        return merchantService.uploadphoto(image, type);
    }


    /**
     * 各类进件图片上传接口-小程序
     *
     * @param file
     * @param photoType
     * @return
     * @throws Exception
     */
    @PostMapping("/merchantRegisterUploadByApplet")
    public ServerResponse merchantRegisterUploadByApplet(
            @RequestBody MultipartFile file,
            @Param(value = "photoType") String photoType,
            HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return merchantService.merchantRegisterUploadByApplet(file, photoType);
    }


    /**
     * 进件回调
     *
     * @param request
     * @throws Exception
     */
    @PostMapping("/merchantRegisterNotice")
    public void merchantRegisterNotice(HttpServletRequest request) throws Exception {
        merchantService.merchantRegisterNotice(request);
    }


    @GetMapping("/findMerchant")
    public ServerResponse findMerchant(Integer type, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return merchantService.findMerchant(type, userInfo);
    }

    /**
     * 查询商户详情-公众号
     */
    @GetMapping("/findMerchantInfo")
    public ServerResponse findMerchantInfo(Merchant merchant, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (ObjectUtils.isEmpty(userInfo)) {
            return ServerResponse.createByErrorMessage(ErrorMsgEnum.USER_INFO_ERROR.getMsg());
        }
        return merchantService.findMerchantInfo(merchant);
    }

    /**
     * 查询商户详情-小程序
     */
    @GetMapping("/findMerchantInfoByApplet")
    public ServerResponse findMerchantInfoByApplet(Merchant merchant, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (ObjectUtils.isEmpty(userInfo)) {
            return ServerResponse.createByErrorMessage(ErrorMsgEnum.USER_INFO_ERROR.getMsg());
        }
        return merchantService.findMerchantInfoByApplet(merchant);
    }

    /**
     * 获取当前登录的商户号
     */
    @GetMapping("/findMerchantListByUserId")
    public ServerResponse findMerchantListByUserId(MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(merchantService.findMerchantListByUserId(merchantDto));
    }

    /**
     * 获取当前登录的商户号实名信息
     */
    @PostMapping("/findMerchantNameInfo")
    public ServerResponse findMerchantNameInfo(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantDto.setUserInfo(userInfo);
        return merchantService.findMerchantNameInfo(merchantDto);
    }


    /**
     * 获取当前登录的账户是否进件商户
     */
    @GetMapping("/findWhetherToImportBusinessByUserId")
    public ServerResponse findWhetherToImportBusinessByUserId(MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantDto.setUserInfo(userInfo);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            return ServerResponse.createBySuccess();
        } else {
            return merchantService.findIdWhetherToImportBusiness(userInfo);
        }
    }

    /**
     * 去签约接口
     */
    @PostMapping("/merchantReRegistration")
    public ServerResponse merchantReRegistration(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return merchantService.merchantReRegistration(merchantDto, userInfo);
    }

    /**
     * 切换商户渠道
     */
    @PostMapping("/switchoverPayCompany")
    public ServerResponse switchoverPayCompany(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (ObjectUtils.isEmpty(userInfo)) {
            return ServerResponse.createByErrorMessage(ErrorMsgEnum.USER_INFO_ERROR.getMsg());
        }
        return merchantService.switchoverPayCompany(merchantDto);
    }

    /**
     * 申请开通乐刷分帐
     */
    @PostMapping("/updateSplit")
    public ServerResponse updateSplit(@RequestBody MerchantDto merchantDto) throws Exception {
        return merchantService.updateSplit(merchantDto);
    }

    /**
     * 开通乐刷分帐获取短信接口-公众号H5
     */
    @PostMapping("/sendCode")
    public ServerResponse sendCode(@RequestBody MerchantDto merchantDto) throws Exception {
        return merchantService.sendCode(merchantDto);
    }

    /**
     * 开通乐刷分帐获取短信接口-小程序
     */
    @PostMapping("/sendCodeByApplet")
    public ServerResponse sendCodeByApplet(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return merchantService.sendCodeByApplet(merchantDto);
    }

    /**
     * 开通乐刷分帐-公众号H5
     */
    @PostMapping("/codeVerify")
    public ServerResponse codeVerify(@RequestBody MerchantDto merchantDto) throws Exception {
        return merchantService.codeVerify(merchantDto);
    }

    /**
     * 开通乐刷分帐-小程序
     */
    @PostMapping("/codeVerifyByApplet")
    public ServerResponse codeVerifyByApplet(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return merchantService.codeVerifyByApplet(merchantDto);
    }

    /**
     * 修改结算卡
     */
    @PutMapping("/updateBankCard")
    public ServerResponse updateBankCard(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (ObjectUtils.isEmpty(userInfo)) {
            return ServerResponse.createByErrorMessage(ErrorMsgEnum.USER_INFO_ERROR.getMsg());
        }
        return merchantService.updateBankCard(merchantDto);
    }

    /**
     * 修改简称
     */
    @PutMapping("/updateMerchantShortName")
    public ServerResponse updateMerchantShortName(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (ObjectUtils.isEmpty(userInfo)) {
            return ServerResponse.createByErrorMessage(ErrorMsgEnum.USER_INFO_ERROR.getMsg());
        }
        return merchantService.updateMerchantShortName(merchantDto);
    }

    /**
     * 查询分账账户
     */
    @PostMapping("/findMerchantSubAccountByUserId")
    public ServerResponse findMerchantSubAccountByUserId(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(merchantService.findMerchantSubAccountByUserId(merchantDto));
    }
    /**
     * 查询投诉列表
     */
    @PostMapping("/findComplainData")
    public ServerResponse findComplainData(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(merchantService.findComplainData(merchantDto));
    }
    /**
     * 查询投诉内容
     */
    @PostMapping("/findMerchantComplaintContent")
    public ServerResponse findMerchantComplaintContent(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantDto.setUserInfo(userInfo);
        return merchantService.findMerchantComplaintContent(merchantDto);
    }
    /**
     * 回复投诉内容
     */
    @PostMapping("/complaintResponse")
    public ServerResponse complaintResponse(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantDto.setUserInfo(userInfo);
        return merchantService.complaintResponse(merchantDto);
    }
    /**
     * 回复投诉内容
     */
    @PostMapping("/complaintComplete")
    public ServerResponse complaintComplete(@RequestBody MerchantDto merchantDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantDto.setUserInfo(userInfo);
        return merchantService.complaintComplete(merchantDto);
    }
}
